package com.dayaokeji.rhythmschoolstudent.utils;

import com.dayaokeji.rhythmschoolstudent.client.common.base.App;
import com.dayaokeji.rhythmschoolstudent.client.mine.account.bean.RemoteHostPicker;
import com.dayaokeji.server_api.ApiUtils;
import com.dayaokeji.server_api.api.ImApi;
import com.dayaokeji.server_api.domain.RequestToken;
import com.litesuits.common.assist.Base64;
import com.orhanobut.logger.Logger;

import org.json.JSONObject;

import java.net.InetAddress;
import java.net.URL;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * ********************************************************************
 * *  Created by Zeno on 2018/6/25
 * ********************************************************************
 * *  version 1.0
 * ********************************************************************
 * *  Copyright (C) dayaokeji.com 2018 -- 2020 All rights reserved.
 * ********************************************************************
 * *
 * ********************************************************************
 */
public class ImUtil {

    public static final int     APP_ID  = 7;
    private static final String APP_SECRET = "0WiCxAU1jh76SbgaaFC7qIaBPm2zkyM1";

    private static final ImApi imApi = ApiUtils.getApi(ImApi.class);
    private static Call<ResponseBody> imApiToken;

    public static void getImToken(long uid, String userName, final AccessTokenCallback accessTokenCallback) {
        final RequestToken requestToken = new RequestToken();
        requestToken.setPlatform_id("android");
        requestToken.setDevice_id(AndroidUtils.getAndroidId(App.getAppContext()));
        requestToken.setUid(uid);
        requestToken.setUser_name(userName);
        Logger.d("request im token ==== "+requestToken.toString());
        imApiToken = imApi.getAccessImToken(requestToken);
        imApiToken.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                Logger.d(response.toString());
                if (response.isSuccessful()) {
                    try{
                        String string = response.body().string();
                        Logger.d("req token ==== "+string);
                        JSONObject jsonObject = new JSONObject(string);
                        if (accessTokenCallback != null) {
                            String token = jsonObject.getString("token");
                            accessTokenCallback.callback(token);
                        }
                    }catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
    }

    public static void cancelReqToken() {
        if (imApiToken != null) {
            imApiToken.cancel();
        }
    }

    public interface AccessTokenCallback{
        void callback(String token);
    }

    public static String generateAuthorization(){
        String md = MD5Util.md5(APP_SECRET);
        String app = APP_ID+":"+md;
        String s = Base64.encodeToString(app.getBytes(), Base64.DEFAULT);
        return "Basic "+s;
    }

    public static String imHost(final String host) {
        String replace = host.replace("/\\", "").replace("http://", "").replace("https://", "");
        int i = replace.indexOf(":");
        return replace.substring(0, i);
    }
}
